﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using HAUtilWpf;
using HA.ModFlow.Domain.DomainLayer;
using HA.ModFlow.Controls.UserControls;

namespace HA.ModFlow.Controls.Windows
{
    /// <summary>
    /// winRecharge.xaml 的交互逻辑
    /// </summary>
    public partial class winRecharge : Window
    {
        public winRecharge(ModFlowData mfData)
        {
            InitializeComponent();
            this.Loaded += new RoutedEventHandler(Window_Loaded);
            this.cbPre.Checked += new RoutedEventHandler(cbPre_Checked);
            this.cbIndex.SelectionChanged += new SelectionChangedEventHandler(cbIndex_SelectionChanged);
            this.cbView.SelectionChanged += new SelectionChangedEventHandler(cbView_SelectionChanged);
            this.btnReset.Click += new RoutedEventHandler(btnReset_Click);
            this.btnOk.Click += new RoutedEventHandler(btnOk_Click);
            this.cbFlux.Content = "补给通量(" + Domain.DomainLayer.UnitConverter.GetLength(mfData.ModelOption.Units.Length) + "/" + Domain.DomainLayer.UnitConverter.GetTime(mfData.ModelOption.Units.Time) + ")";
            this._recharges = new PCRecharge(mfData);


            //显示第一个应力期的时间点
            this.labStart.Content = "0.0";
            if (Recharges.TimePoint.Length <= 1)
                return;
            else
                this.labFinish.Content = Recharges.TimePoint[1].ToString();

            this.cbIndex.Items.Clear();
            this.cbIndex.ItemsSource = this.Recharges.PeriodIndex;

            //当Use previous 选中时不可修改各属性值
            Binding bindEabled = new Binding();
            bindEabled.ElementName = "cbPre";
            bindEabled.Path = new PropertyPath("IsChecked");
            bindEabled.Converter = new NotConvertor();
            this.btnReset.SetBinding(Button.IsEnabledProperty, bindEabled);

            Binding bindEable = new Binding();
            bindEable.ElementName = "cbPre";
            bindEable.Path = new PropertyPath("IsChecked");
            bindEable.Converter = new BoolToVisityConverter();
            myArray.dgData.SetBinding(DataGrid.VisibilityProperty,bindEable);



            Binding bindOption = new Binding();
            bindOption.Source = Recharges.Recharge;
            bindOption.Path = new PropertyPath("Option");
            bindOption.Converter = new RechargeToIntConverter();
            bindOption.Mode = BindingMode.TwoWay;
            cbOption.SetBinding(ComboBox.SelectedIndexProperty, bindOption);

            //初始选择 Flux，double
            this.myArray.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this.Recharges.FluxData[cbIndex.SelectedIndex]);
            this.myArray.SetDoubleData(this.Recharges.FluxData[cbIndex.SelectedIndex]);
        }

        #region 字段及属性

        PCRecharge _recharges;

        public PCRecharge Recharges
        {
            get { return _recharges; }
            set { _recharges = value; }
        }

        #endregion

        #region 事件
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.DataContext = this.Recharges;      
        }

        private void cbPre_Checked(object sender, RoutedEventArgs e)
        {
            int index = this.cbIndex.SelectedIndex;
            if (index > 0)
            {
                this.Recharges.FluxData[index] = (double[,])this.Recharges.FluxData[index].Clone();
                if (this.cbOption.SelectedIndex == 1)
                {
                    this.Recharges.IndicatorData[index] = (int[,])this.Recharges.IndicatorData[index - 1].Clone();
                }
            }
        }

        private void cbIndex_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (this.cbIndex.SelectedIndex != -1)
            {
                int index = this.cbIndex.SelectedIndex;

                string start = Recharges.TimePoint[index].ToString();
                string finish = Recharges.TimePoint[index + 1].ToString();

                if (start.Contains("."))
                {
                    this.labStart.Content = start;
                }
                else
                {
                    this.labStart.Content = start + ".0";
                }

                if (finish.Contains("."))
                {
                    this.labFinish.Content = finish;
                }
                else
                {
                    this.labFinish.Content = finish + ".0";
                }


                Binding bindMut = new Binding();
                bindMut.Source = _recharges.Recharge.Periods[index];
                bindMut.Path = new PropertyPath("Multiplier");
                bindMut.Converter = new NumberToStringConverter();
                this.txtMultiplier.SetBinding(TextBox.TextProperty, bindMut);

                Binding bindUsepre = new Binding();
                bindUsepre.Source = _recharges;
                bindUsepre.Path = new PropertyPath("IsUsePre["+index+"]");
                this.cbPre.SetBinding(CheckBox.IsCheckedProperty,bindUsepre);


                if (this.cbView.SelectedIndex == 0)
                {
                    this.myArray.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this.Recharges.FluxData[index]);
                    this.myArray.SetDoubleData(this.Recharges.FluxData[cbIndex.SelectedIndex]);

                }

                if (this.cbView.SelectedIndex == 1)
                {

                    this.myArray.dgData.ItemsSource = PCGridArrayLayer<int>.GetBindable2DArray<int>(this.Recharges.IndicatorData[index]);
                    this.myArray.SetIntData(this.Recharges.IndicatorData[cbIndex.SelectedIndex]);
                }
               
            }
        }

        private void cbView_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbView.SelectedIndex == 0)
            {
                this.myArray.dgData.ItemsSource = PCGridArrayLayer<double>.GetBindable2DArray<double>(this.Recharges.FluxData[cbIndex.SelectedIndex]);
                this.myArray.SetDoubleData(this.Recharges.FluxData[cbIndex.SelectedIndex]);
            }
            else if (cbView.SelectedIndex == 1)
            {
                this.myArray.dgData.ItemsSource = PCGridArrayLayer<int>.GetBindable2DArray<int>(this.Recharges.IndicatorData[cbIndex.SelectedIndex]);
                this.myArray.SetIntData(this.Recharges.IndicatorData[cbIndex.SelectedIndex]);
            }
        }

        private void btnReset_Click(object sender, RoutedEventArgs e)
        {

            if (this.cbView.SelectedIndex == 0)
            {

                winResetValue winReset = new winResetValue(DataType.DOUBLE);
                winReset.Owner = this;
                winReset.ShowDialog();

                if (winReset.DialogResult == true)
                {
                    ResetDoubleData(Convert.ToDouble(winReset.txtResetValue.Text));
                }
            }
            else if (this.cbView.SelectedIndex == 1)
            {
                winResetValue winReset = new winResetValue(DataType.INT);
                winReset.Owner = this;
                winReset.ShowDialog();

                if (winReset.DialogResult == true)
                {
                    double xxx = Convert.ToDouble(winReset.txtResetValue.Text);

                    ResetIntData(Convert.ToInt32(xxx));
                }
            }
        }

        private void btnOk_Click(object sender, RoutedEventArgs e)
        {
            this.Recharges.ConfirmToDataCenter();
            this.DialogResult = true;
            this.Close();
        }

        #endregion

        #region 辅助方法
        //重置层为单一值
        public void ResetIntData(int data)
        {
            int index = this.cbIndex.SelectedIndex;

            for (int i = 0; i < this.Recharges.IndicatorData[index].GetLength(0); i++)
                for (int j = 0; j < this.Recharges.IndicatorData[index].GetLength(1); j++)
                {
                    this.Recharges.IndicatorData[index][i, j] = data;
                }

            int k = index + 1;

            while (k < this.Recharges.IsUsePre.Count && this.Recharges.IsUsePre[k])
            {
                for (int i = 0; i < this.Recharges.IndicatorData[k].GetLength(0); i++)
                    for (int j = 0; j < this.Recharges.IndicatorData[k].GetLength(1); j++)
                    {
                        this.Recharges.IndicatorData[k][i, j] = data;
                    }
                k++;
            }
            this.myArray.dgData.Items.Refresh();
        }

        public void ResetDoubleData(double data)
        {
            int index = this.cbIndex.SelectedIndex;

            for (int i = 0; i < this.Recharges.FluxData[index].GetLength(0); i++)
                for (int j = 0; j < this.Recharges.FluxData[index].GetLength(1); j++)
                    this.Recharges.FluxData[index][i, j] = data;
            int k = index + 1;

            while (k < this.Recharges.IsUsePre.Count && this.Recharges.IsUsePre[k])
            {
                for (int i = 0; i < this.Recharges.FluxData[k].GetLength(0); i++)
                    for (int j = 0; j < this.Recharges.FluxData[k].GetLength(1); j++)
                    {
                        this.Recharges.FluxData[k][i, j] = data;
                    }
                k++;
            }
            this.myArray.dgData.Items.Refresh();
        }

        #endregion 

        private void cbOption_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.cbView.SelectedIndex = 0;
        }
    }

    public class RechargeToIntConverter: IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            switch ((RECHARGEOPTION)value)
            {
                case RECHARGEOPTION.TOPLAYER:          return 0;
                case RECHARGEOPTION.VERTICALCELLS:     return 1;
                case RECHARGEOPTION.HIGHESTACTIVECELL: return 2;
                default:                               throw new NotImplementedException();
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            switch ((int)value)
            {
                case 0:  return RECHARGEOPTION.TOPLAYER;
                case 1:  return RECHARGEOPTION.VERTICALCELLS;
                case 2:  return RECHARGEOPTION.HIGHESTACTIVECELL;
                default: throw new NotImplementedException();
            }
        }
    }
    public class BoolToVisityConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {

            switch ((bool)value)
            {
                case true: return Visibility.Hidden;
                case false: return Visibility.Visible;
                default: throw new NotImplementedException();
            }

        }

        public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
        {
            throw new NotImplementedException();
        }
    }

}
